/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.account.core.util;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import info.peigen.hotpot.business.router.facade.enums.ChannelCode;
import info.peigen.hotpot.common.core.enums.Currency;
import info.peigen.hotpot.common.service.base.exception.BusinessException;

import static info.peigen.hotpot.business.router.facade.enums.ChannelCode.Weixin;
import static info.peigen.hotpot.common.core.enums.Currency.CNY;

/**
 * <b>(CreateAccountUtil)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/10/16
 */
@SuppressWarnings("unused")
public class CreateAccountUtil {
    public final static String CUSTOMER_ACCOUNT_TAG            = "00";
    public final static String PARTNER_ACCOUNT_TAG             = "33";
    public final static String SYSTEM_ACCOUNT_TAG_INTERMEDIATE = "55";
    public final static String SYSTEM_ACCOUNT_TAG_FEE          = "66";
    public final static String SYSTEM_ACCOUNT_TAG_PROVISIONS   = "88";

    /** yyyyMMddHHmmss+(0000-2999)+0156 */
    public static String createCustomerAccountId() {
        return createAccountId(SYSTEM_ACCOUNT_TAG_PROVISIONS, Weixin, CNY);
    }

    /** yyyyMMddHHmmss+3333+0156 */
    public static String createPartnerAccountId() {
        return createAccountId(SYSTEM_ACCOUNT_TAG_PROVISIONS, Weixin, CNY);
    }

    public static String createPartnerAccountId(ChannelCode code, Currency currency) {
        return createAccountId(SYSTEM_ACCOUNT_TAG_PROVISIONS, code, currency);
    }

    /** yyyyMMddHHmmss+8888+0156 */
    public static String createProvisionsAccountId() {
        return createAccountId(SYSTEM_ACCOUNT_TAG_PROVISIONS, Weixin, CNY);
    }

    public static String createProvisionsAccountId(ChannelCode code, Currency currency) {
        return createAccountId(SYSTEM_ACCOUNT_TAG_PROVISIONS, code, currency);
    }

    /** yyyyMMddHHmmss+6666+0156 */
    public static String createFeeAccountId() {
        return createAccountId(SYSTEM_ACCOUNT_TAG_FEE, Weixin, CNY);
    }

    public static String createFeeAccountId(ChannelCode code, Currency currency) {
        return createAccountId(SYSTEM_ACCOUNT_TAG_FEE, code, currency);
    }

    /** yyyyMMddHHmmss+5555+0156 */
    public static String createIntermediateAccountId() {
        return createAccountId(SYSTEM_ACCOUNT_TAG_INTERMEDIATE, Weixin, CNY);
    }

    public static String createIntermediateAccountId(ChannelCode code, Currency currency) {
        return createAccountId(SYSTEM_ACCOUNT_TAG_INTERMEDIATE, code, currency);
    }

    private static String createAccountId(String tag, ChannelCode code, Currency currency) {
        String channelIndex;
        if (code.isInList(Weixin)) {
            channelIndex = "0";
        } else if (code.isInList(ChannelCode.YIJIFU)) {
            channelIndex = "1";
        } else {
            throw new BusinessException("暂不执行的渠道");
        }

        return StrUtil.concat(true, DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_PATTERN),
                tag,
                RandomUtil.randomNumbers(2),
                channelIndex,
                ObjectUtil.isNotNull(currency) ? currency.getIdentify() : CNY.getIdentify());
    }
}